# -*- coding: utf-8 -*-
from openerp.osv import fields,osv
from openerp import tools
from openerp.tools.translate import _
from openerp.modules.registry import RegistryManager
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DATE_FORMAT
import datetime
from dateutil.relativedelta import relativedelta

class pm_port(osv.osv):
    _name = 'pm.port'
    _columns = {
        'project_id':fields.many2one('pm.project',string='所属管理处'),
        'area_id':fields.many2one('pm.project.area',string='所属区域',domain="[('project_id','=',project_id)]"),
        'name':fields.char(string='车位编号',size=100),
        'kind':fields.selection([('underground','地下'),('upground','地上')],string='车位类别'),
        'manager_type':fields.many2one('pm.parameter',string='管理类别',domain="[('category_code','=','GLLB')]"),
        'port_type':fields.many2one('pm.parameter',string='管理类别',domain="[('category_code','=','CWLB')]"),
        'sale_price':fields.float(string='预售价格'),
        'rent_price':fields.float(string='预租价格'),
        'position':fields.char(string='车位位置',size=500),
        'area':fields.float(string='车位面积'),
        'state':fields.selection([('empty','空闲'),('saled','已售'),('rented','已租')],string='当前状态'),
        'remark':fields.text(string='备注'),
    }

    _defaults = {
        'state':'empty',
    }

pm_port()

class pm_port_saleorder(osv.osv):
    _name = 'pm.port.saleorder'
    
    def create(self,cr,uid,data,context=None):
        id = super(pm_port_saleorder,self).create(cr,uid,data,context=context)
        if id:
            self.pool.get('pm.port').write(cr,uid,data['port_id'],{'state':'saled'},context=context)
        return id

    _columns = {
        'name':fields.many2one('res.partner',string='产权人',required=True,domain="[('is_owner','=',True)]"),
        'port_id':fields.many2one('pm.port',string='车位'),
        'port_position':fields.related('port_id','position',type='char',string='车位位置'),
        'price':fields.float(string='实售价格',required=True),
        'car_number':fields.char(string='车牌号码',required=True),
        'port_number':fields.char(string='停车证号',required=True),
    }
pm_port_saleorder()

class pm_port_rentorder(osv.osv):
    _name = 'pm.port.rentorder'
    _columns = {
        'project_id':fields.many2one('pm.project',string='所属管理处'),
        'area_id':fields.many2one('pm.project.area',string='所属区域',domain="[('project_id','=',project_id)]"),
        'port_id':fields.many2one('pm.port',string='选择车位',domain="[('state','=','empty'),('area_id','=',area_id)]"),
        'name':fields.char(string='合同编号',size=100,required=True),
        'rent_id':fields.many2one('res.partner',string='使用人'),
        'sign_date':fields.date(string='签订日期'),
        'car_number':fields.char(string='车牌号码',size=100),
        'port_number':fields.char(string='停车证号',size=100),
        'rent_start_date':fields.date(string='租期开始日'),
        'rent_end_date':fields.date(string='租期结束日'),
        'rent_month':fields.integer(string='承租月数'),
        'month_price':fields.float(string='月服务费'),
        'state':fields.selection([('active','已生效'),('notactive','未生效'),('end','已终止')],string='当前状态'),
        'remark':fields.text(string='备注'),
    }

    _defaults = {
        'state':'notactive',
    }
pm_port_rentorder()

class pm_car_inout(osv.osv):
    _name = 'pm.car.inout'
    _columns = {
        'project_id':fields.many2one('pm.project',string='所属管理处'),
        'area_id':fields.many2one('pm.project.area',string='所属区域',domain="[('project_id','=',project_id)]"),
        'port_id':fields.many2one('pm.port',string='选择车位',domain="[('state','=','empty'),('area_id','=',area_id)]"),
        'name':fields.char(string='停车牌号',size=100,required=True),
        'car_number':fields.char(string='车牌号码',size=100),
        'car_owner':fields.char(string='车主姓名', size=100),
        'in_time':fields.datetime(string='入场时间'),
        'out_time':fields.datetime(string='出厂时间'),
        'watch':fields.many2one('hr.employee',string='值班人'),
        'remark':fields.text(string='备注'),
    }
pm_car_inout()

